package io.tec.cloud.algorithm.c01_v20220601;

public class SortTestUtil {


    public static void main(String[] args) throws Exception {
        int testTimes = 10000;
        int maxValue = 500;
        int maxLength = 500;
        for (int i = 0; i < testTimes; i++) {
            int[] original = generatorRandomArray(maxValue, maxLength);
            p(original);
            int[] arr1 = copyArray(original);
            int[] arr2 = copyArray(original);
            int[] arr3 = copyArray(original);
//            System.out.println("==========");
//            p(original);
            BubbleSort.bubbleSort(arr1);
            SelectionSort.selectionSort(arr2);
            InsertionSort.insertionSort(arr3);
//            p(arr1);
//            p(arr2);
//            p(arr3);
            if (
                !isEq(arr1, arr2)
                ||
                !isEq(arr2, arr3)
                ||
                !isEq(arr1, arr3)
            ) {
                System.out.println("error");
                break;
            }
        }
        System.out.println("finish");
    }


    public static int[] generatorRandomArray(int maxValue, int maxLength) {
        int length = 16;//random(maxLength);
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = (int) (Math.random() * maxValue) - (int) (Math.random() * maxValue);

        }
        return arr;
    }

    public static int random(int num) {
        return (int) (Math.random() * num) + 1;
    }

    public static int[] copyArray(int[] arr) {
        int[] newArray = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            newArray[i] = arr[i];
        }
        return newArray;
    }

    public static boolean isEq(int[] arr1, int[] arr2) {
        if (arr1 == null || arr2 == null) {
            return false;
        }

        if (arr1.length != arr2.length) {
            return false;
        }

        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static void p(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ", ");
        }
        System.out.println();
    }
}
